package com.smartwasser.yunhtgl.apphtgl.presenter;

import android.os.Handler;

import com.jph.takephoto.model.TImage;
import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat5Bean;
import com.smartwasser.yunhtgl.apphtgl.network.RetrofitApi;
import com.smartwasser.yunhtgl.apphtgl.network.Retrofits;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal11View;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal1View;
import com.smartwasser.yunhtgl.apphtgl.view.SPNormal6View;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SPNormal6PresenterImpl implements SPNormal6Presenter {
    private SPNormal6View sp6NormalView;
    private Handler mHandler = new Handler();

    public SPNormal6PresenterImpl(SPNormal6View sp6NormalView) {
        this.sp6NormalView = sp6NormalView;
    }

    @Override
    public void getType1Sp1Normal(final String JSESSIONID, final String id) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getType1Sp1Great(JSESSIONID, id)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<SPGreat1Bean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                sp6NormalView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final SPGreat1Bean object) {
                sp6NormalView.success(object);
            }
        });
    }

    @Override
    public void getFlMessage(String JSESSIONID) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getFlMessage(JSESSIONID)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<FlMessageBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                sp6NormalView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final FlMessageBean flMessageBean) {
                sp6NormalView.successGetFlMessage(flMessageBean);
            }
        });
    }

    @Override
    public void getCommitPhoto(String JSESSIONID, String id,String procInsId,List<TImage> updateMedia) {
        ArrayList<String> pathList = new ArrayList<>();
        for (int i=0;i<updateMedia.size();i++){
            pathList.add(updateMedia.get(i).getCompressPath());
        }
        Map<String, RequestBody> bodyMap = new HashMap<>();
        if (pathList.size()>0){
            for (int i=0;i<pathList.size();i++){
                File file = new File(pathList.get(i));
                bodyMap.put("file"+i+"\"; filename=\""+file.getName(),RequestBody.create(MediaType.parse("image/png"),file));
            }
        }
//        MultipartBody.Part[] part =new MultipartBody.Part[pathList.size()];
//        for (int i=0;i<pathList.size();i++){
//            File file = new File(pathList.get(i));
//            RequestBody requestFile =
//                    RequestBody.create(MediaType.parse("multipart/form-data"), file);
//            part[i] =MultipartBody.Part.createFormData("file", file.getName(), requestFile);
//        }
        Retrofits.getInstance().createReq(RetrofitApi.class).updatePhoto(JSESSIONID, id,procInsId,bodyMap)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BaseBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                sp6NormalView.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final BaseBean baseBean) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        sp6NormalView.successCommitSH(baseBean);
                    }
                }, 1000);
                Logger.e("onNext");
            }
        });
    }
}